<?php

include 'DBconn.php';

class StateMaster {

    public function getAllStates() {
        $result = mysql_query("select * from state_header where  activestatus=1") or die(mysql_error());
        $res_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VoStateMaster();
            $tmp->country_fk = (int) $row->country_fk;
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->state_name = $row->state_name;
            $tmp->cre_userid_fk = (int) $row->cre_userid_fk;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $res_arr[] = $tmp;
        }
        mysql_free_result($result);
        return $res_arr;
    }

    public function getStatesByFK($country_fk) {
        $result = mysql_query("select * from state_header where country_fk='$country_fk' and activestatus=1") or die(mysql_error());
        $res_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VoStateMaster();
            $tmp->country_fk = (int) $row->country_fk;
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->state_name = $row->state_name;
            $tmp->cre_userid_fk = (int) $row->cre_userid_fk;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $res_arr[] = $tmp;
        }
        mysql_free_result($result);
        return $res_arr;
    }

    public function addNewState($VObject)
	{
        $cls = array();
        $state_name = ucfirst($VObject->state_name);
        $query = "insert into state_header(country_fk,state_name,cre_userid_fk,cre_datetime,activestatus) values('$VObject->country_fk','$state_name','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllStates();
    }

    public function editState($VObject)
	{
        $state_name = ucfirst($VObject->state_name);
        $query = "update state_header set country_fk='$VObject->country_fk',state_name='$state_name' where serialnumber='$VObject->serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllStates();
    }

    public function deleteState($serialnumber) {
        $s = mysql_query("update state_header set activestatus=0 where serialnumber='$serialnumber'") or die(mysql_error());
        return "true";
    }

}

class VoStateMaster {
    var $country_fk;
    var $serialnumber;
    var $state_name;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;

}

?>